{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import joblib\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_01 = joblib.load('train_HA_feature/train_01.lz4')\n",
    "train_02 = joblib.load('train_HA_feature/train_02.lz4')\n",
    "train_03 = joblib.load('train_HA_feature/train_03.lz4')\n",
    "\n",
    "test_01 = joblib.load('./test_HA_feature/test_01.lz4')\n",
    "test_02 = joblib.load('./test_HA_feature/test_02.lz4')\n",
    "test_03 = joblib.load('./test_HA_feature/test_03.lz4')\n",
    "test_04 = joblib.load('./test_HA_feature/test_04.lz4')\n",
    "test_05 = joblib.load('./test_HA_feature/test_05.lz4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [],
   "source": [
    "SCALE_DICT = dict(PCA_T2__mean=1/10, PCA_T2__mean_diff=10, PCA_T2__delta=1, PCA_T2__delta_diff=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [],
   "source": [
    "def window_feature(df, has_label=False):\n",
    "    \n",
    "    data = df.copy()\n",
    "    feature_cols = [col for col in data.columns if col not in ['CL', 'RULR']]\n",
    "    data = data[feature_cols]\n",
    "    # 缩放\n",
    "    for key,val in SCALE_DICT.items():\n",
    "        data[key] = data[key].values * val\n",
    "        \n",
    "    point_cnt = data.shape[0]-10+1\n",
    "    x = [data.loc[i:i+9].values for i in range(point_cnt)]\n",
    "    x = np.array(x)\n",
    "    if has_label:\n",
    "        y = df['RULR'].rolling(window=10, center=False, min_periods=10).mean().dropna().reset_index(drop=True).values\n",
    "        return x, y \n",
    "    else:\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [],
   "source": [
    "# train\n",
    "x_01, y_01 = window_feature(train_01, has_label=True)\n",
    "x_02, y_02 = window_feature(train_02, has_label=True)\n",
    "x_03, y_03 = window_feature(train_03, has_label=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train = np.vstack([x_01, x_02, x_03]).reshape(-1,10,4,1)\n",
    "y_train = np.hstack([y_01, y_02, y_03]).reshape(-1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [],
   "source": [
    "# test\n",
    "x_01 = window_feature(test_01, has_label=False)\n",
    "x_02 = window_feature(test_02, has_label=False)\n",
    "x_03 = window_feature(test_03, has_label=False)\n",
    "x_04 = window_feature(test_04, has_label=False)\n",
    "x_05 = window_feature(test_05, has_label=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_test = np.vstack([x_01, x_02, x_03, x_04, x_05]).reshape(-1,10,4,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists('./window_feature'):\n",
    "    os.mkdir('./window_feature')\n",
    "x_train.tofile('./window_feature/x_train')\n",
    "y_train.tofile('./window_feature/y_train')\n",
    "x_test.tofile('./window_feature/x_test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.38905609893065"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
